import json
import jsonpath

d = {
    "error_code": 0,
    "stu_info":
    [
        {"id": 223,
         "name": "安妮",
         "sex": "女",
         "classname": "212期",
         "data": {
             "name": "测试1",
             "data": {
                 "name": "测试2"
             }
         }
         },
        {"id": 224,
         "name": "景龙",
         "sex": "男",
         "classname": "212期"
         },
        {"id": 225,
         "name": "MS226",
         "classname": "212期"
         }
    ]
}

print(type(d))
# # 字典的取值方法
# name = d['stu_info'][0]['name']
# print(name)

# 匹配所有节点下的name
names = jsonpath.jsonpath(d, '$..name')
print(names)

# 匹配对应节点下的name
name1 = jsonpath.jsonpath(d, '$.stu_info[0].name')
print(name1)

# 匹配前面两条信息下的一级name
name2 = jsonpath.jsonpath(d, '$.stu_info[:2].name')
print(name2)

data5=jsonpath.jsonpath(d, "$.stu_info[?(@.sex=='女')]")
print("匹配性别为女的学生信息:", data5)

data6 = jsonpath.jsonpath(d, '$.stu_info[?(@.sex)]')
print("有性别字段的数据为：", data6)